# ==================================
#
#  Code for replicating:
# "Positioning Under Alternative Electoral Systems: Evidence From Japanese Candidate Election Manifestos"
#  Amy Catalinac, NYU
#
# ==================================



# ==================================
# Calculating district-level dispersion 
# (sample: all cands prior to ER; LDP, DPJ, and NFP after ER)

load("covars_ideal_points.Rdata") # saved as ideal, sep_theta in 5th column

# calculating dispersion using all cands prior to 1996:

ideal.district.pre <- ideal[ideal$year<1996,]
district.var.pre <- by(ideal.district.pre, list(ideal.district.pre$ku, ideal.district.pre$year), 
                       function(x) var(x[,5]))
# saved as "dispersion_in_districts_preER_all.csv"

# NB: after reading this out, we assigned:
# ku="129" (M of 1) an "NA" in 1986 and 1990 elections and renamed district column "ku".

# Calculating dispersion using cands from LDP, DPJ, and NFP after 1996:
ideal.district.post <- ideal[ideal$year>1993 & (ideal$pty==1|ideal$pty==52|ideal$pty==54),]
district.var.post <- by(ideal.district.post, list(ideal.district.post$ku, ideal.district.post$year), 
                        function(x) var(x[,5]))
# saved as "dispersion_in_districts_postER_majorcands.csv"

# NB: After reading this out, we made the followning "NA":
# ku=1703 in 1996 (JCP cand was miscoded)
# ku=2811 in 1996 (no manifesto for LDP cand)
# ku=2308 in 2005 (no manifesto for NNP cand)
# and renamed district column "ku".

rm(list=ls())




# ==================================
# Identifying "competitive candidates" prior to ER; 
# (defined as: those who were one of top M+1 vote-getters in the same district in the previous election)

load("ideal_with_83.Rdata") # called ideal.with.83
# (Reed data on all cands from 1983-1994)

ideal.district.pre <- ideal.with.83
years <- unique(ideal.district.pre$year)

# identify top vote getters in each year
all.the.years <- matrix(as.character(NA), ncol=3, nrow=0)
for(j in 1:length(years)){
  year.dset <- ideal.district.pre[ideal.district.pre$year==years[j],]
  kus <- unique(year.dset$ku)
  tops <- matrix(as.character(NA), ncol=3, nrow=0)
  for(i in 1:length(kus)){
    subku <- year.dset[year.dset$ku==kus[i],]
    subkuo <- subku[order(subku$mmd.vote, decreasing=T),]
    subkuotop <- subkuo[1:(unique(subkuo$m)+1),]
    tops <- rbind(tops, cbind(as.character(subkuotop$���O), subkuotop$ku, subkuotop$year))
}
  all.the.years <- rbind(all.the.years, tops)
}
all.the.years <- data.frame(cand.name=as.character(all.the.years[,1]), ku=all.the.years[,2], year=all.the.years[,3])
all.the.years$comp <- 1
cand.ku <- paste(all.the.years$cand.name, all.the.years$ku, sep="_")
all.the.years$cand.ku <- cand.ku

# Of the 641 competitive candidates in 1983, how many ran in same district in 1986?
to.check.86 <- all.the.years[all.the.years$year==1983,]
ideal.district.pre.86 <- ideal.district.pre[ideal.district.pre$year==1986,]
name.ku <- paste(ideal.district.pre.86$���O, ideal.district.pre.86$ku, sep="_")
ideal.district.pre.86$name.ku <- name.ku

# Subset 1986 data by those who are "competitive"
comp.86 <- ideal.district.pre.86[ideal.district.pre.86$name.ku%in%to.check.86$cand.ku,]

# Of the 642 competitive candidates in 1986, how many ran in same district in 1990?
to.check.90 <- all.the.years[all.the.years$year==1986,]
ideal.district.pre.90 <- ideal.district.pre[ideal.district.pre$year==1990,]
name.ku <- paste(ideal.district.pre.90$���O, ideal.district.pre.90$ku, sep="_")
ideal.district.pre.90$name.ku <- name.ku

# Subset 1990 data by those who are "competitive"
comp.90 <- ideal.district.pre.90[ideal.district.pre.90$name.ku%in%to.check.90$cand.ku,]

# Of the 642 competitive candidates in 1990, how many ran in same district in 1993?
to.check.93 <- all.the.years[all.the.years$year==1990,]
ideal.district.pre.93 <- ideal.district.pre[ideal.district.pre$year==1993,]
name.ku <- paste(ideal.district.pre.93$���O, ideal.district.pre.93$ku, sep="_")
ideal.district.pre.93$name.ku <- name.ku
table(ideal.district.pre.93$name.ku%in%to.check.93$cand.ku)
comp.93 <- ideal.district.pre.93[ideal.district.pre.93$name.ku%in%to.check.93$cand.ku,]
dim(ideal.district.pre.93) 
dim(comp.93) 

# stack the competitive candidates in these three elections together:
ideal.district.pre.comp <- rbind(comp.86, comp.90, comp.93)
save(ideal.district.pre.comp, file="comp_cands_preER.Rdata")

rm(list=ls())





# ==================================
# Calculating dispersion with competitive candidates prior to ER;

load(file="comp_cands_preER.Rdata") # called ideal.district.pre.comp, 
district.var.pre <- by(ideal.district.pre.comp, list(ideal.district.pre.comp$ku, ideal.district.pre.comp$year), 
                       function(x) var(x[,5]))
# saved as "dispersion_in_districts_preER_compcands.csv"
# NB: after reading this out, we assigned:
# ku="129" (M of 1) an "NA" in 1986 and 1990 elections and renamed district column "ku".

rm(list=ls())



